/*********** NOTES HEADER *********** 
*This program adds quarters in which the patient is not observed with a claim to the dataset
*  We allow these people to not progress into health outcomes.
*  This should help precision. This is for the matched sample

Datasets used: 
 (1) vertmergers
 (2) PS-logit-BENE-QUARTER_FINAL2005-2012  
 (3) PSPats_logit_final
 
 
Datasets created:
 (1) PS-logit-BENE-FILL-IN-QTR_FINAL2005-2012


************************************/ 

set more off
capture log close
clear all
global origData "N:\MedicareClaims-P045601-BE"
global dataIn "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-In\"
global dataOut "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global dataProp "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Data-Out/PropScore/Patients"
global logs "N:\MedicareClaims-P045601-BE\Work\hosp_retro\health_out\Logs\PropScore\Patients"
global dpath "N:\MedicareClaims-P045601-BE\Work\ay_data"
global skapath "N:\MedicareClaims-P045601-BE\Work\ska"

adopath +  N:\SIL-Common\estout
adopath +  N:\SIL-Common\outreg2

***Merger timing 

tempfile vmerger_npi
tempfile vmerger_upin

use "$dataIn/vertmergers"
keep  *ID q_of_merger  
rename npi_ID vm_PROVIDER1
save `timing', replace

clear

***End merger timing 



tempfile bene_ds

	*Data where patient is observed with a claim during the quarter
	use BENE_ID quarter BENE_* propScoreId using "$dataProp/PS-logit-BENE-QUARTER_FINAL2005-2012.dta", replace
	  keep if propScoreId~=.
	  sort BENE_ID BENE_D*
	  by BENE_ID: replace BENE_D=BENE_D[1]

	*Consistent birth dates
	egen birth_date=min(BENE_BIRTH_DT), by(BENE_ID)
	 format birth_date %td_M_D_Y
	 drop BENE_BIRTH_DT
	 rename birth_date BENE_BIRTH_DT
	  
	*Patients in the dataset
	preserve
	 drop if quarter==.
	 egen begdate=min(quarter), by(BENE_ID)
	 drop quarter

	 duplicates drop
	  
	save `bene_ds', replace
	clear

	*Quarters in the dataset
	restore
	keep quarter
	 drop if year(quarter)<2005
	 drop if year(quarter)>2012
	 duplicates drop

	*Cross of people and quarters
	cross using `bene_ds'
	 format begdate %tdD_m_Y 

	*Limits based on dying and entry into the dataset
	gen dropvar=quarter > mdy(quarter(BENE_D)*3,1,year(BENE_D))
	 replace dropvar=2 if quarter<begdate
	 drop if dropvar>0
	 drop dropvar
	 drop BENE_COUNTY_CD BENE_ZIP_CD BENE_RACE_CD BENE_SEX_IDENT_CD
	  
	duplicates drop 
 

 
 *Merge all quarters onto list
merge 1:m BENE_ID quarter using "$dataProp/PS-logit-BENE-QUARTER_FINAL2005-2012.dta"
 drop if _merge==2
 drop merger
 
 
*Variables in the model:
*postmerger(q_of_merger) sex_pat vmerger rc_white(race) nyear(quarter) p_age(BENE_BIRTH_DT) fmp pspcl30 pspcl06 pspcl69 pspcl18 pspcl48 pspcl93 pspclOther mxicd*_chronic* state(STATE) propScoreId

	encode STATE, gen(STATE_encode)
	*Xs that don't vary
	sort BENE_ID quarter
	rename mxicd8_acute mxacute8
	foreach i of varlist q_of_merger SEX* merger RACE* BENE_BIRTH_DT BENE_DEATH_DT psps* mxhyp* mxdiab* mxicd*_chronic* mxicd*_acute* propScoreId STATE_encode vm_PROVIDER* urban* hosp* lor* em* vm* {
	by BENE_ID : replace `i' = `i'[_n-1] if `i'==.
	}

	foreach i of varlist BENE_COUNTY_CD BENE_ZIP_CD BENE_SEX_ BENE_RACE_CD {
	by BENE_ID : replace `i' = `i'[_n-1] if `i'==""
	}

	*Ys 
	foreach i of varlist mx* {
	by BENE_ID : replace `i' =`i'[_n-1]  if `i'==.
	}

	foreach i of varlist new* {
	 replace `i' =0  if `i'==.
	 }

	egen pspOther=rowmax(psps41 psps10 psps97 psps02 psps29 psps35 psps13 psps83 psps43 psps04 psps49 psps16 psps01 psps39 psps66 psps26 psps46 psps25 psps47 psps70 psps73 psps65 psps77 psps90 psps14 psps92 psps38 psps44 psps94 psps63 psps03 psps68 psps09 psps33 psps24 psps64 psps81 psps60 psps21 psps78 psps80 psps37 psps40 psps28 psps72 psps36 psps89 psps82 psps98 psps67 psps87 psps76 psps71 psps91 psps12 psps32 psps19 psps85 psps99 psps45 psps84 psps17 psps23 psps75 psps86 psps42 psps74 psps15 psps88 psps62 psps27 psps79 psps95 pspsA5 pspsC0 psps54 pspsA0 pspsC1)
	*Xs that vary
	foreach i of varlist fmp psps30 psps06 psps69 psps18 psps48 psps93 pspOther doc_Cnt mnfirmsize mxfirmsize fs* {
	by BENE_ID : replace `i' =0 if `i'==.
	}

	*Quarter and postmerger
	gen qtr=qofd(quarter)
	format qtr %tq

	gen postmerger=q_of_merger<qtr  
	replace postmerger=0 if postmerger==.
	replace postmerger=0 if q_of_merger==.

	capture drop age
	gen age=round( (quarter- BENE_BIRTH_DT)/365.25,1)

	capture drop _merge 
	merge m:1 BENE_ID using "$dataProp/PSPats_logit_final.dta"


	*For movers, find the relevant Geography (keep the moved-to Geography)
	capture drop tag
	duplicates tag BENE_ID quarter, gen(tag)

	replace zip_pat = substr(zip_pat,1,5)
	replace zip_pat = substr(BENE_ZIP_CD,1,5) if zip_pat==""

	*ZIP
	sort BENE_ID quarter
	by BENE_ID : gen str9 ZIPlastpd= BENE_ZIP_CD[_n-1] if tag>0
	 by BENE_ID quarter: replace ZIPlastpd= ZIPlastpd[1] if tag>0
	 
	by BENE_ID : gen str5 ziplastpd= substr(BENE_ZIP_CD[_n-1],1,5) if tag>0
	 by BENE_ID quarter: replace ziplastpd= ZIPlastpd[1] if tag>0
	 
	 by BENE_ID : gen str9 ZIPnextpd= BENE_ZIP_CD[_n+1] if tag>0
	 by BENE_ID quarter: replace ZIPnextpd= ZIPnextpd[_N] if tag>0

	by BENE_ID : gen str5 zipnextpd= substr(BENE_ZIP_CD[_n-1],1,5) if tag>0
	 by BENE_ID quarter: replace zipnextpd = zipnextpd[1] if tag>0
	 
	by BENE_ID : gen str9 CTYlastpd= BENE_COUNTY_CD[_n-1] if tag>0
	 by BENE_ID quarter: replace CTYlastpd= CTYlastpd[1] if tag>0
	 
	*County
	by BENE_ID : gen str9 CTYnextpd= BENE_COUNTY_CD[_n+1] if tag>0
	 by BENE_ID quarter: replace CTYnextpd= CTYnextpd[_N] if tag>0
	 by BENE_ID quarter: replace BENE_COUNTY_CD= CTYnextpd if tag>0
	 
	by BENE_ID : gen str9 STATEnextpd= STATE_CODE[_n+1] if tag>0
	 by BENE_ID quarter: replace STATEnextpd= STATEnextpd[_N] if tag>0
	 by BENE_ID quarter: replace STATE_CODE= STATEnextpd if tag>0

	by BENE_ID : gen statenextpd= STATE_encode[_n+1] if tag>0
	 by BENE_ID quarter: replace statenextpd= statenextpd[_N] if tag>0
	 by BENE_ID quarter: replace STATE_encode= statenextpd if tag>0
 
 
 *base duplicates on whether zip is equal to last period (no keep)
	gen keepvar=1
	 replace keepvar=1-(ZIPlastpd==BENE_ZIP_CD) if  tag>0 
	 replace keepvar=1-(ziplastpd==zip_pat) if  tag>0 
	 replace keepvar=1-(CTYlastpd==BENE_COUNTY_CD) if  tag>0 
	 
	drop tag 

*there are some observations with multiple physician/outcome variables during the period. Take the max
		keep if keepvar==1
		capture drop tag
		drop keepvar ZIPlast ziplastpd zipnextpd CTYnext CTYlast BENE_ZIP_CD STATEnextpd statenextpd

		tempfile somedups
		duplicates drop
		save `somedups', replace

		tempfile ids
		drop mx* psp* new*
		duplicates drop

		duplicates tag BENE_ID quarter, gen(tag)
		 replace zip_pat = substr(ZIPnext ,1,5)
		drop ZIPnext
		duplicates drop
		sort BENE_ID quarter
		capture drop _merge
	save `ids', replace
	clear

 use `somedups'
	keep BENE_ID quarter psp* mx* new* begdate
	collapse (max) psp* mx* new* , by(BENE_ID quarter)

	sort BENE_ID quarter
	merge 1:1 BENE_ID quarter using `ids'
	drop _merge tag

save "$dataProp/PS-logit-BENE-FILL-IN-QTR_FINAL2005-2012.dta", replace




